package cn.pconline.autoclub.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.gelivable.dao.GeliDao;
import org.gelivable.dao.GeliOrm;
import org.gelivable.dao.SqlBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import cn.pconline.autoclub.entity.ClubStyle;
import cn.pconline.autoclub.entity.Style;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author zhoutianhuai
 */
public class ClubStyleService {

    @Autowired
    private GeliOrm geliOrm;
    @Autowired
    private GeliDao geliDao;
    private static final Logger log = LoggerFactory.getLogger(ClubStyleService.class);

    /**
     * 根据clubId获得车友会风格样式
     *
     * @return
     */
    public ClubStyle getClubStyleByClubId(long clubId) {
        try {
            return geliDao.find(ClubStyle.class, clubId);
        } catch (DataAccessException e) {
            return null;
        }
    }

    /**
     * 根据clubId获得车友会风格样式
     *
     * @return
     */
    public Style getStyleByClubId(long clubId) {
        ClubStyle cs = getClubStyleByClubId(clubId);
        Style style;
        if (cs == null) {
            style = findDefaultStyle();
        } else {
            style = new Style();
            style.setCreateAt(cs.getCreateAt());
            style.setCreateBy(cs.getCreateBy());
            style.setIsDefault(Style.STYLE_DEFAULT);
            style.setStyleContent(cs.getStyleContent());
            style.setStyleId(cs.getStyleId());
            style.setType(cs.getType());
        }
        return style;
    }

    /**
     * 更新车友会风格样式
     *
     * @param clubStyle
     */
    public void updateClubStyle(ClubStyle clubStyle) {
        geliDao.update(clubStyle);
    }

    /**
     * 创建车友会默认风格样式
     *
     * @param clubId
     * @param styleId
     * @param styleContent
     * @param type
     * @param adminId
     */
    public void createDefaultClubStyle(ClubStyle clubStyle) {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql(" select * from ").appendSql(geliOrm.getTableName(Style.class));
        sql.appendSql(" where is_default = ").appendValue(Style.STYLE_DEFAULT);
        sql.appendSql(" and type = ").appendValue(Style.STYLE_TYPE_TZ);
        Style style = geliDao.findFirst(Style.class, sql.getSql(), sql.getValues());
        
        if(style != null){
	        JSONObject styleContentJson = JSONObject.parseObject(style.getStyleContent());
	        styleContentJson.put(ClubStyle.SHOWIMAGE, "");
	        clubStyle.setStyleContent(styleContentJson.toJSONString());
	        clubStyle.setStyleId(style.getStyleId());
	        clubStyle.setType(ClubStyle.TYPE_DEFAULT);
	        geliDao.create(clubStyle);
        }
    }

    /**
     * 根据styleId获取风格样式
     *
     * @param styleId
     * @return
     */
    public Style getStyleByStyleId(long styleId) {
        try {
            return geliDao.find(Style.class, styleId);
        } catch (DataAccessException e) {
            return null;
        }
    }

    /**
     * 根据不同的类型，获取
     *
     * @param type
     * @return
     */
    public List<Style> listStyleByType(int type) {
        deleteCashe(type);
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql(" select style_id from ").appendSql(geliOrm.getTableName(Style.class));
        sql.appendSql(" where type = ").appendValue(type).appendSql(" order by level asc");
        try {
            return geliDao.list(Style.class, sql.getSql(), sql.getValues());
        } catch (EmptyResultDataAccessException e) {
            return Collections.emptyList();
        }
    }

    public void deleteCashe(int type) {

        SqlBuilder sql = new SqlBuilder();
        sql.appendSql(" select style_id from ").appendSql(geliOrm.getTableName(Style.class));
        sql.appendSql(" where type = ").appendValue(type);
        try {
            List<Style> list = geliDao.list(Style.class, sql.getSql(), sql.getValues());
            for (Style style : list) {
                geliDao.deleteCache(style.getClass(), style.getStyleId());
            }
        } catch (EmptyResultDataAccessException e) {
        }
    }

    /**
     * 获取默认套装
     *
     * @param isDefault
     * @return
     */
    public Style findDefaultStyle() {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql(" select style_id from ").appendSql(geliOrm.getTableName(Style.class));
        sql.appendSql(" where is_default = ").appendValue(Style.STYLE_DEFAULT);
        sql.appendSql(" and type = ").appendValue(Style.STYLE_TYPE_TZ);
        try {
            return geliDao.list(Style.class, sql.getSql(), sql.getValues()).get(0);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    /**
     * 查询默认封面图
     *
     * @return
     */
    public Style findDefaultCoverImage() {
        SqlBuilder sql = new SqlBuilder();
        sql.appendSql(" select style_id from ").appendSql(geliOrm.getTableName(Style.class));
        sql.appendSql(" where is_default = ").appendValue(Style.STYLE_DEFAULT);
        sql.appendSql(" and type = ").appendValue(Style.STYLE_TYPE_FM);
        try {
            return geliDao.list(Style.class, sql.getSql(), sql.getValues()).get(0);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }

    /**
     * 创建车友会样式
     *
     * @param clubStyle
     *
     */
    public void createClubStyle(ClubStyle clubStyle) {
        geliDao.create(clubStyle);
    }

    /**
     * 更新车友会样式
     *
     * @param clubStyle
     *
     */
    public void ClubStyle(ClubStyle clubStyle) {
        geliDao.update(clubStyle);
    }

    /**
     * 将样式的Json格式转为字符串，分别保存独立的属性值
     *
     * @param list
     * @return
     */
    public List<Style> setStyleContentToString(List<Style> list) {
        List<Style> styleList = new ArrayList<Style>();
        for (Style style : list) {
            try {
                JSONObject jsonObject = JSONObject.parseObject(style.getStyleContent());
                String cssString = jsonObject.getString(ClubStyle.CSS).toString();
                String coverString = jsonObject.getString(ClubStyle.COVERIMAGE).toString();
                String showImage = jsonObject.getString(ClubStyle.SHOWIMAGE).toString();
                String background = jsonObject.getString(ClubStyle.BACKGROUND).toString();
                style.setCssString(cssString);
                style.setCoverString(coverString);
                style.setShowImage(showImage);
                style.setBackground(background);
                styleList.add(style);
            } catch (JSONException exception) {
            }
        }
        return styleList;
    }
}
